package common

const (
	OK      = 0
	FAIL    = 1
	ERROR   = -1 // 服务器异常
	NODATE  = 10 // 没有数据
	NOPARAM = 20 // 缺少参数
	NOPERMISSION = 30 // 没有权限
)

var MSG = map[int]string{
	OK:      "成功",
	FAIL:    "失败",
	ERROR:   "服务异常",
	NODATE:  "没有数据",
	NOPARAM: "参数不合法",
	NOPERMISSION:"缺少权限",
}

type Coder struct {
	Code    int         `json:"code"`
	Message string      `json:"message"`
	Data    interface{} `json:"data"`
}

func NewCoderBool(ok bool) Coder{
	if ok{
		return NewCoderOK()
	}else{
		return NewCoderFail()
	}
}

func NewCoderOK() Coder {
	return Coder{
		Code:    OK,
		Message: MSG[OK],
	}
}

func NewCoderFail() Coder {
	return Coder{
		Code:    FAIL,
		Message: MSG[FAIL],
	}
}

func NewCoderDefault(data interface{}) Coder {
	c := Coder{
		Code:    OK,
		Message: MSG[OK],
		Data:    data,
	}
	if data == nil {
		c = Coder{
			Code:    FAIL,
			Message: MSG[FAIL],
		}
	}
	return c
}

func NewCoderString(data string) Coder {
	c := Coder{
		Code:    OK,
		Message: MSG[OK],
		Data:    data,
	}
	if data == "" {
		c = Coder{
			Code:    FAIL,
			Message: MSG[FAIL],
		}
	}
	return c
}

func NewCoder(code int, data interface{}) Coder {
	c := Coder{
		Code:    code,
		Message: MSG[code],
		Data:    data,
	}
	return c
}

func NewCoderTip(code int) Coder {
	c := Coder{
		Code:    code,
		Message: MSG[code],
	}
	return c
}
